package com.edu.javaweb.controller;

import com.edu.javaweb.pojo.Admin;
import com.edu.javaweb.pojo.Student;
import com.edu.javaweb.service.AdminService;
import com.edu.javaweb.service.StudentService;
import com.edu.javaweb.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private StudentService studentService;
    @RequestMapping("/login")
    public String login(LoginVo loginVo, HttpSession session){
        String type = loginVo.getType();
        if (type.equals("管理员")){
            String account = loginVo.getAccount();
            if (adminService.adminLogin(new Admin(null,account,loginVo.getPassword()),session)){
                return "admin";
            }
        }else {
            Student student=studentService.findByName(loginVo.getAccount());
            if (student.getPassword().equals(loginVo.getPassword())){
                session.setAttribute("student",student);
                return "student";
            }
        }
        return "index";
    }
}
